package com.sixth.system.service.impl;/**
 * @author 633yh
 * @version 1.0
 */

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sixth.core.entity.Role;
import com.sixth.core.entity.RoleUserEntity;
import com.sixth.core.vo.R;
import com.sixth.system.mapper.RoleMapper;
import com.sixth.system.mapper.RoleUserMapper;
import com.sixth.system.service.RoleService;
import com.sixth.system.vo.RoleVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author     ：Stu YH
 * @date       ：Created in 2024/8/16 14:40
 * @description：
 * @modified By：
 * @version:
 */
@Service
public class RoleServiceimpl implements RoleService {
    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private RoleUserMapper roleUserMapper;
    public R<Page<Role>> selectByPage(RoleVo roleVo) {
        Page<Role> page = new Page<>(roleVo.getPageNum(), roleVo.getPageSize());
        QueryWrapper<Role> wrapper = new QueryWrapper<>();
        if (roleVo.getRoleId()!= null){
            wrapper.eq("role_id", roleVo.getRoleId());
        }
        if (roleVo.getRoleName() != null) {
            wrapper.like("role_name", roleVo.getRoleName());
        }
        if (roleVo.getRoleCode() != null) {
            wrapper.like("role_code", roleVo.getRoleCode());
        }
        if (roleVo.getDateRange() != null && roleVo.getDateRange().size() == 2) {
            wrapper.between("create_time", roleVo.getDateRange().get(0), roleVo.getDateRange().get(1));
        }
        if (roleVo.getStatus() != null) {
            wrapper.eq("status", roleVo.getStatus());
        }
        wrapper.eq("del_flag", 0);
        wrapper.orderByAsc("role_sort");  // 显示顺序
        System.out.println("role_sort:~~~~~~~~~~~~`"+roleVo.getRoleSort());
        Page<Role> roleIPage = roleMapper.selectPage(page, wrapper);
        return R.ok(roleIPage);
    }
    // 查询所有可用角色
    @Override
    public R<List<Role>> selectAllRole() {
        QueryWrapper<Role> wrapper = new QueryWrapper<>();
        wrapper.eq("status", 0);
        wrapper.eq("del_flag", 0);
        List<Role> roleList = roleMapper.selectList(wrapper);
        return R.ok(roleList);
    }

    @Override
    public R<Role> getById(Long roleId) {
        return R.ok(roleMapper.selectById(roleId));
    }

    @Override
    public boolean add_update_role(Role role) {
        role.setCreateTime(new java.util.Date());
        return roleMapper.insertOrUpdate(role);
    }

    @Override
    public R batchDelete_role(Long[] roleIds) {
        // 将角色id数组转换成Role对象列表，设置删除标记
        List<Role> colect = Arrays.stream(roleIds).map(id -> {
            Role role = new Role();
            role.setRoleId(id);
            role.setDelFlag("1");
            return role;
        }).collect(Collectors.toList());
        //更新用户状态为已删除
        roleMapper.updateById(colect);
        return R.ok();
    }

    @Override
    public R removeById(Long roleId) {
        // 逻辑删除 修改删除标记
        Role role = new Role();
        role.setRoleId(roleId);
        role.setDelFlag("1");
        return R.ok(roleMapper.updateById(role));
    }

    @Override
    public R findRolesByUserId(Long userId) {
        Long[] roleIds = roleUserMapper.getRoleIdsByUserId(userId);
        return R.ok(roleIds);
    }

    @Override
    public R saveRoleUser(Long currentUserId, Long[] roleIds) {
        QueryWrapper<RoleUserEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", currentUserId);
        roleUserMapper.delete(wrapper);
        System.out.println("currentUserId:"+currentUserId);
        for(Long roleId: roleIds){
            RoleUserEntity roleUserEntity = new RoleUserEntity();
            roleUserEntity.setRoleId(roleId);
            roleUserEntity.setUserId(currentUserId);
            roleUserMapper.insert(roleUserEntity);
        }
        return R.ok();

    }


}